package cn.yudao.ymcc.validannotion;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidPhone implements ConstraintValidator<phone,String> {
    private static final String PHONE_REGEX =
            "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9]))\\d{8}$";

    // 初始化时候运行
    @Override
    public void initialize(phone constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    // 做业务判断
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {

        if (!StringUtils.isBlank(s)){
            // 创建正则表达
            Pattern compile = Pattern.compile(PHONE_REGEX);
            // 使用正则表达式进行校验
            Matcher matcher = compile.matcher(s);
            // 得到校验结果
            boolean matches = matcher.matches();
            return matches;
        }
        return false;
    }


}
